package com.hyyt.shopjsp.home.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hyyt.shopjsp.common.controller.BaseController;
import com.hyyt.shopjsp.home.pojo.ShopNavigation;
import com.hyyt.shopjsp.home.service.IShopNavigationService;
import com.hyyt.shopjsp.util.common.Page;
import com.hyyt.shopjsp.util.common.ResultMsg;
import com.hyyt.shopjsp.util.common.Utils;
import com.hyyt.shopjsp.util.json.FastJsonUtils;

/**
 * Copyright (c) 2016 ShopJsp. All Rights Reserved.
 * ============================================================================
 * 版权所有 2011 - 今 北京华宇盈通科技有限公司，并保留所有权利。
 * ----------------------------------------------------------------------------
 * 提示：在未取得SHOPJSP商业授权之前，您不能将本软件应用于商业用途，否则SHOPJSP将保留追究的权力。
 * ----------------------------------------------------------------------------
 * 官方网站：http://www.shopjsp.com
 * ============================================================================
 * @author Wang Youchun on 2017/03/28 0019下午 6:38.
 */
@Controller
@RequestMapping("/back/navigation")
public class ShopNavigationController extends BaseController {
    private Logger logger = Logger.getLogger(ShopNavigationController.class);
    private String basePath = "/back/shop/navigation/";

    @Resource
    private IShopNavigationService shopNavigationService;//首页导航条service

    /**
	 * 进入首页导航条管理
	 *
	 * @return 进入指定页面
	 */
	@RequestMapping("gotoNavigationPage")
	public String gotoNavigationPage(){
		return basePath+"navigationList";
	}

	/**
	 * 查询所有首页导航条
	 * @param page
	 * @param response
	 */
	@RequestMapping(value="listNavigation",method=RequestMethod.POST)
	@ResponseBody
	public void listNavigation(String navigationName, String isShowOnBar, Page page, HttpServletResponse response){
		Map<String, Object> param = new HashMap<>();
		if (Utils.objectIsNotEmpty(navigationName)) {//首页导航条名称
			param.put("navigationName", navigationName.trim());
		}
		if (Utils.objectIsNotEmpty(isShowOnBar)) {//是否显示
			param.put("isShowOnBar", isShowOnBar);
		}
		param.put("sortColumns", "navigationId desc");
		int totalRecordCount =shopNavigationService.countShopNavigation(param);
		page.setPageInfo(page.getPageSize(), totalRecordCount, page.getCurrentPage());
		List<ShopNavigation> list =shopNavigationService.queryShopNavigation(param, page.getPageRecordBeginIndex(), page.getPageSize());
		param.clear();
		param.put("total", totalRecordCount);// total键 存放总记录数，必须的
		param.put("rows", list);// rows键 存放每页记录 list
		ResultMsg.create(param, response);  //把内容发送到页面
	}

	/**
     * 根据首页导航条id获得对象
     *
     * @param navigationId
     */
    @RequestMapping(value = "getNavigationObject", method = RequestMethod.POST)
    @ResponseBody
    public ResultMsg getNavigationObject(Integer navigationId) {
		if (Utils.objectIsNotEmpty(navigationId)) {
			ShopNavigation navigation = shopNavigationService.loadShopNavigation(navigationId);
			return ResultMsg.create(true, null, FastJsonUtils.toJSONString(navigation));
		}else {
			return ResultMsg.create(false);
		}
	}

    /**
     * 保存或者修改用户信息
     *
     * @param navigation
     * @return
     */
    @RequestMapping(value = "saveOrUpdateNavigation", method = RequestMethod.POST)
    @ResponseBody
    public ResultMsg saveOrUpdateNavigation(ShopNavigation navigation) {
		if (Utils.objectIsNotEmpty(navigation)) {
			if (Utils.objectIsEmpty(navigation.getNavigationId())) {
				shopNavigationService.saveShopNavigation(navigation);
			} else {
				shopNavigationService.updateShopNavigation(navigation);
			}
			return ResultMsg.create(true);
		}else {
			return ResultMsg.create(false);
		}
	}

    /**
     * 根据传入的首页导航条id
     *
     * @param ids
     */
    @RequestMapping(value = "deleteNavigation", method = RequestMethod.POST)
    @ResponseBody
    public ResultMsg deleteNavigation(String ids) {
		if (Utils.objectIsNotEmpty(ids)) {
			shopNavigationService.deleteShopNavigation(Integer.parseInt(ids));
			return ResultMsg.create(true);
		}else {
			return ResultMsg.create(false);
		}
	}
}
